package map;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @description:
 * @author: zhaoyinglin
 * @date: 2024/3/11
 */
public class LruImplementsByDeque {

    public static void main(String[] args) {
        // LRUCache lruCache = new LRUCache(2);
        // lruCache.put(2, 1);
        // lruCache.put(1, 1);
        // lruCache.put(2, 3);
        // lruCache.put(4, 1);
        //
        // System.out.println(lruCache.get(1));
        // System.out.println(lruCache.get(2));
        //
        // Deque<Integer> deque = new LinkedList<>();
        // deque.addFirst(1);
        // deque.addFirst(1);
        // System.out.println(deque);
        //
        // deque.remove(1);
        // System.out.println(deque.size());

        double a = 1.00;
        System.out.println(a % 1);
    }

    static class LRUCache {

        private final Map<Integer, Integer> cacheValue;

        private final int capacity;

        private final Deque<Integer> deque;

        public LRUCache(int capacity) {
            this.capacity = capacity;
            this.deque = new LinkedList<>();
            this.cacheValue = new HashMap<>();
        }

        public int get(int key) {

            deque.remove(key);

            deque.addFirst(key);

            Integer value = cacheValue.get(key);

            return value == null ? -1 : value;
        }

        public void put(int key, int value) {

            cacheValue.put(key, value);

            if (!cacheValue.containsKey(key)) {
                deque.addFirst(key);
            } else {
                deque.remove(key);
                deque.addFirst(key);
            }

            if (deque.size() > capacity) {
                int removed = deque.removeLast();
                cacheValue.remove(removed);
            }
        }
    }

}
